package com.huika.renrench.ui;

import java.util.ArrayList;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.model.ProjectImgs;
import com.huika.renrench.view.TitleBarHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * 图片查看
 * @author fanxing 创建于 Mar 30, 2015
 */
public class ImagePageViewsActivity extends BaseAct {
	public static final String INIT_INDEX = "initIndex";
	public static final String IMGS = "IMGS";
	
	private ViewPager pager;
	private MyPageAdapter adapter;
	public int initIndex;
	private ArrayList<ProjectImgs> mPhotoPathList;
	private DisplayImageOptions options;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_imgs);
		new TitleBarHelper(this, -1, R.string.project_imgs);
		
		options = new DisplayImageOptions.Builder()
    		.showImageOnLoading(R.drawable.icon_default_big) // resource or drawable
    		.showImageForEmptyUri(R.drawable.icon_default_big) 
    		.showImageOnFail(R.drawable.icon_default_big)
    		.cacheInMemory(true)// 开启内存缓存
    		.cacheOnDisk(true) // 开启硬盘缓存
    		.resetViewBeforeLoading(false).build();
		
		pager = (ViewPager) findViewById(R.id.pager);
		
		mPhotoPathList = (ArrayList<ProjectImgs>) getIntent().getSerializableExtra(IMGS);
		
		adapter = new MyPageAdapter();// 构造adapter
		pager.setAdapter(adapter);// 设置适配器
		pager.setCurrentItem(initIndex);
	}

	private class MyPageAdapter extends PagerAdapter {

		public int getCount() {// 返回数量
			return mPhotoPathList==null ? 0 : mPhotoPathList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			ProjectImgs imgItem = mPhotoPathList.get(position);
			String imgUrl = imgItem.getShowBigImageUrl();
			
			View root = inflater.inflate(R.layout.item_img, container, false);
			ImageView iv = (ImageView) root.findViewById(R.id.iv);
			View pb = root.findViewById(R.id.pb_loading);
			iv.setTag(pb);
			ImageLoader.getInstance().displayImage(imgUrl, iv, options, imageLoadingListener);
			container.addView(root);
			
			return root;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}

	private ImageLoadingListener imageLoadingListener = new ImageLoadingListener() {
		
		@Override
		public void onLoadingStarted(String arg0, View arg1) {
			View pb = (View) arg1.getTag();
			pb.setVisibility(View.VISIBLE);
			((ImageView)arg1).setImageResource(R.drawable.icon_default_big);
		}
		
		@Override
		public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
			View pb = (View) arg1.getTag();
			pb.setVisibility(View.GONE);
			((ImageView)arg1).setImageResource(R.drawable.icon_default_big);
		}
		
		@Override
		public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
			View pb = (View) arg1.getTag();
			pb.setVisibility(View.GONE);
			((ImageView)arg1).setImageBitmap(arg2);
		}
		
		@Override
		public void onLoadingCancelled(String arg0, View arg1) {
			View pb = (View) arg1.getTag();
			pb.setVisibility(View.GONE);
			((ImageView)arg1).setImageResource(R.drawable.icon_default_big);
		}
	};

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		
	}
}
